<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="com.example.entity.User" %>
<%@ page import="com.example.utils.DButil" %>
<%@ page import="com.example.dao.UserDao" %>
<%@ page import="com.example.dao.impl.UserDaoimpl" %>
<%@ page import="com.example.entity.UserName" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/12/25
  Time: 17:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title></title>
    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
    <link rel="stylesheet" href="./css/手机主页面.css">
</head>
<%
    ArrayList<Integer> list = (ArrayList<Integer>) request.getSession().getAttribute("collect");
    String CONTENT = null;
    String TITLE = null;
    String USERID = null;
    String TEXT = null;
    String PICTURE = null;
    int TIEZIID;


    HttpSession session1 = request.getSession();
    User user1 = (User) session1.getAttribute("user");

    // 数据库连接信息
//    String url = "jdbc:mysql://localhost:3306/db1";
//    String user = "root";
//    String password = "123456";

    boolean collectflag = false;//判断是否存在收藏
    try {
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 建立连接
//        Connection conn = DriverManager.getConnection(url, user, password);
        Connection conn = DButil.getConnection();
//        System.out.println("conn = " + conn);
        PreparedStatement ps = conn.prepareStatement("select * from upload");

        PreparedStatement ps1;
        ResultSet rs1;

        // 创建SQL语句
        ResultSet rs = ps.executeQuery();

        String ti = "bx-like";
        String ts = "bxs-star";
        String tc = "bx-coin";



        while (rs.next()) {


            boolean flag = false;
            // 获取查询结果
            TITLE = rs.getString("TITLE");
            CONTENT = rs.getString("CONTENT");
            USERID = rs.getString("USERID");
            TEXT = rs.getString("TEXT");
            PICTURE = rs.getString("PICTURE");
            TIEZIID = rs.getInt("TIEZIID");


            UserDao userDao = new UserDaoimpl();
            UserName username = userDao.findByUserId(Integer.parseInt(USERID));

            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == (Integer) TIEZIID) {
                    flag = true;
                    collectflag = true;
                }
            }

            ps1 = conn.prepareStatement("CALL proc_tiezibyuserid(?)");
            ps1.setInt(1,user1.getUserId());
            rs1 = ps1.executeQuery();
            while(rs1.next()){
                if (rs1.getInt("TIEZIID") == TIEZIID) {
                    if (rs1.getString("DIANZAN").equals("0")) {
                        ti = "bx-like";
                    } else {
                        ti = "bxs-like";
                    }
                    if (rs1.getString("SHOUCANG").equals("0")) {
                        ts = "bx-star";
                    } else {
                        ts = "bxs-star";
                    }
                    if (rs1.getString("TOUBI").equals("0")) {
                        tc = "bx-coin";
                    } else {
                        tc = "bxs-coin";
                    }
                }
            }

            if(flag){
                out.print("<div class='CONTENT'>");
                out.print("<span style='border-radius: 1px;'>" + TIEZIID + "</span>");
                out.print("<span style='border-radius: 1px;padding-left: 5px'>" + username.getUsername() + "</span>");
                out.print("<h1>" + TITLE + "</h1>");
                out.print("<div class='TEXT'>");
                out.print(TEXT);
                out.print("</div>");
                out.print(PICTURE);
                out.print("<div class='sbox'>");
                out.print("<i class='bx " + ti + "'></i>");
                out.print("<i class='bx " + ts + "'></i>");
                out.print("<i class='bx " + tc + "'></i>");
                out.print("</div>");
                out.print("</div>");
                ti = "bx-like";
                ts = "bxs-star";
                tc = "bx-coin";
            }
        }
        out.print("<div id='popup'>");
        out.print("</div>");

    } catch (Exception e) {
        e.printStackTrace();
    }
    if (!collectflag){
        out.print("<div style='position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);font-size:60px'>无收藏</div>");
    }
%>
<body>

</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    // 获取所有bx-like类的图标
    var likeIcons = document.querySelectorAll('i[class$="-like"]');
    likeIcons.forEach(function (icon) {
        icon.addEventListener("click", function () {
            var span = this.parentElement.parentElement.querySelector('span');
            var CheckLike = icon.classList.contains("bx-like");
            var CheckStar = icon.parentElement.querySelector('i[class$="-star"]').classList.contains("bxs-star");
            var CheckCoin = icon.parentElement.querySelector('i[class$="-coin"]').classList.contains("bxs-coin");
            console.log(CheckCoin)
            if (CheckLike) {
                icon.classList = 'bx bxs-like'
            } else {
                icon.classList = 'bx bx-like'
            }
            var popup = document.getElementById('popup');
            popup.textContent = CheckLike ? "点赞收到!   " : "取消点赞!";
            popup.style.display = 'block';
            setTimeout(function () {
                popup.style.display = 'none';
            }, 1200);

            // 发送AJAX请求到服务器
            $.ajax({
                type: "POST",
                url: "sendtotiezi.jsp", // 服务器端处理页面
                data: {
                    CheckLike:CheckLike,
                    CheckStar:CheckStar,
                    CheckCoin:CheckCoin,
                    TIEZIID:span.textContent,
                    USERID:<%=user1.getUserId()%>,
                    FLAG:"like",
                },
            });
        });

    });

    // 获取所有bx-star类的图标
    var starIcons = document.querySelectorAll('i[class$="-star"]');
    starIcons.forEach(function (icon) {
        icon.addEventListener("click", function () {
            var span = this.parentElement.parentElement.querySelector('span');
            var CheckLike = icon.parentElement.querySelector('i[class$="-like"]').classList.contains("bxs-like");
            var CheckStar = icon.classList.contains("bx-star");
            var CheckCoin = icon.parentElement.querySelector('i[class$="-coin"]').classList.contains("bxs-coin");
            if (CheckStar) {
                icon.classList = 'bx bxs-star'
            } else {
                icon.classList = 'bx bx-star'
            }
            var popup = document.getElementById('popup');
            popup.textContent = CheckStar ? "收藏成功!" : "取消收藏!";
            popup.style.display = 'block';
            setTimeout(function () {
                popup.style.display = 'none';
            }, 1200);

            // 发送AJAX请求到服务器
            $.ajax({
                type: "POST",
                url: "sendtotiezi.jsp", // 服务器端处理页面
                data: {
                    CheckLike:CheckLike,
                    CheckStar:CheckStar,
                    CheckCoin:CheckCoin,
                    TIEZIID:span.textContent,
                    USERID:<%=user1.getUserId()%>,
                    FLAG:"star",
                },
            });
        });
    });

    // 获取所有bx-coin类的图标
    var coinIcons = document.querySelectorAll('i[class$="-coin"]');
    coinIcons.forEach(function (icon) {
        icon.addEventListener("click", function () {
            var span = this.parentElement.parentElement.querySelector('span');
            var CheckLike = icon.parentElement.querySelector('i[class$="-like"]').classList.contains("bxs-like");
            var CheckStar = icon.parentElement.querySelector('i[class$="-star"]').classList.contains("bxs-star");
            var CheckCoin = icon.classList.contains("bx-coin");
            if (CheckCoin) {
                icon.classList = 'bx bxs-coin'
            } else {
                icon.classList = 'bx bx-coin'
            }
            var popup = document.getElementById('popup');
            popup.textContent = CheckCoin ? "投币成功!" : "取消投币!";
            popup.style.display = 'block';
            setTimeout(function () {
                popup.style.display = 'none';
            }, 1200);

            // 发送AJAX请求到服务器
            $.ajax({
                type: "POST",
                url: "sendtotiezi.jsp", // 服务器端处理页面
                data: {
                    CheckLike:CheckLike,
                    CheckStar:CheckStar,
                    CheckCoin:CheckCoin,
                    TIEZIID:span.textContent,
                    USERID:<%=user1.getUserId()%>,
                    FLAG:"coin",
                },
            });
        });
    });
</script>
</html>